package com.landray.kmss.yggs.todonotify;

import com.koron.common.core.util.SpringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 解析配置文件
 */
@Data
@RefreshScope
@Component
@Slf4j
public class WebServiceConfig {

    //private static WebServiceConfig cfg = new WebServiceConfig();

    // Web服务的URL
    @Value("${oa.todo.address}")
    private String address;

    // Web服务接口
    @Value("${oa.todo.service_class}")
    private Class serviceClass;

    // Web服务标识
    @Value("${oa.todo.service_bean}")
    private String serviceBean;

    // 用户
    @Value("${oa.todo.user}")
    private String user;

    // 密码
    @Value("${oa.todo.password}")
    private String password;

    // 是否发送待办
    @Value("${oa.todo.send:false}")
    private Boolean send;

    /*private WebServiceConfig() {
        //loadCfg();
    }*/

    public static WebServiceConfig getInstance() {
        return SpringUtils.getBean(WebServiceConfig.class);
    }

    /**
     * 解析配置文件
     */
    private void loadCfg() {
        Properties prop = new Properties();
        InputStream confStream = this.getClass().getResourceAsStream(
                "client.properties");

        try {
            prop.load(confStream);
            this.address = prop.getProperty("address");
            String serviceClassName = prop.getProperty("service_class");
            this.serviceClass = Class.forName(serviceClassName);
            this.serviceBean = prop.getProperty("service_bean");
            this.user = prop.getProperty("user");
            this.password = prop.getProperty("password");

        } catch (Exception e) {
            // TODO 自动生成的catch块
            log.error(e.getMessage(), e);
        } finally {
            try {
                confStream.close();
            } catch (IOException e) {
                // TODO 自动生成的catch块
                log.error(e.getMessage(), e);
            }
        }

    }

}
